package com.hystrix;

import com.hystrix.collapser.MyHystrixCollapser;
import com.hystrix.command.IsolationCommand;
import com.hystrix.entity.Person;
import com.netflix.config.ConfigurationManager;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * 请先启动当前项目的Spirng Boot
 * Created by earl on 2018/5/1.
 */
public class TestCollapser {


    public static void main(String [] args) throws ExecutionException, InterruptedException {
        testCollapser();
    }

    //测试合并请求
    private static void testCollapser() throws ExecutionException, InterruptedException {
        //收集一秒内产生的请求，合并为一个命令执行
        // 收集 1 秒内发生的请求，合并为一个命令执行
        ConfigurationManager.getConfigInstance().setProperty(
                "hystrix.collapser.default.timerDelayInMilliseconds", 1000);
        // 请求上下文
        HystrixRequestContext context = HystrixRequestContext
                .initializeContext();
        // 创建请求合并处理器
        MyHystrixCollapser c1 = new MyHystrixCollapser("Angus");
        MyHystrixCollapser c2 = new MyHystrixCollapser("Crazyit");
        MyHystrixCollapser c3 = new MyHystrixCollapser("Sune");
        MyHystrixCollapser c4 = new MyHystrixCollapser("Paris");
        // 异步执行
        Future<Person> f1 = c1.queue();
        Future<Person> f2 = c2.queue();
        Future<Person> f3 = c3.queue();
        Future<Person> f4 = c4.queue();
        System.out.println(f1.get());
        System.out.println(f2.get());
        System.out.println(f3.get());
        System.out.println(f4.get());
        context.shutdown();
    }




}
